package com.kedou.app;

import com.kedou.domain.Fund;
import com.kedou.domain.ProjectItem;
import com.kedou.net.PostConfirmFund;
import com.kedou.net.PostGoFund;
import com.kedou.util.NetworkUtil;
import com.kedou.util.PreferencesUtil;
import com.kedou.util.Util;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 确认认筹 界面
 * 
 * @author zhouchao
 * 
 */
public class ConfirmFundActivity extends BaseActivity implements
		OnClickListener {
	/**
	 * 左侧 返回按钮
	 */
	private Button mBtnBack;
	/**
	 * 中间 Title
	 */
	private TextView mTvTitle;
	/**
	 * 右侧 设置
	 */
	private ImageView mImgSetting;
	/**
	 * 项目名称
	 */
	private TextView mTvProjectName;
	/**
	 * 众筹订单
	 */
	private TextView mTvProjectOrder;
	/**
	 * 订单状态
	 */
	private TextView mTvOrderState;
	/**
	 * 去网银支付
	 */
	private Button mBtnGotoPay;
	private Bundle mBundle;
	private ProjectItem mProjectItem;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mBundle = getIntent().getBundleExtra("project");
		if (mBundle != null) {
			mProjectItem = mBundle.getParcelable("project_item");
		}
		setContentView(R.layout.activity_confirmfund);
		initView();
		setListener();
		getDataFromServer();
	}

	/**
	 * 初始化View
	 */
	private void initView() {
		mBtnBack = (Button) findViewById(R.id.btn_back);
		mBtnBack.setVisibility(View.VISIBLE);
		mTvTitle = (TextView) findViewById(R.id.title);
		mTvTitle.setText(R.string.title_renchou_success);
		mImgSetting = (ImageView) findViewById(R.id.img_setting);
		mImgSetting.setVisibility(View.VISIBLE);
		mTvProjectName = (TextView) findViewById(R.id.tv_project_name);
		mTvProjectOrder = (TextView) findViewById(R.id.tv_project_order);
		mTvOrderState = (TextView) findViewById(R.id.tv_order_state);
		mBtnGotoPay = (Button) findViewById(R.id.go_to_pay);
	}

	/**
	 * 设置监听器
	 */
	private void setListener() {
		mBtnGotoPay.setOnClickListener(this);
		mBtnBack.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_back:
			finish();
			break;
		case R.id.go_to_pay:
			Intent intent = new Intent(this, PayInformationActivity.class);
			this.startActivity(intent);
			break;
		}
	}

	/**
	 * 从网站获取数据
	 */
	private void getDataFromServer() {
		mProgressDialog.show();
		if (NetworkUtil.isNetworkAvailable(ConfirmFundActivity.this)) {
			new PostConfirmFund(PreferencesUtil.getString(
					ConfirmFundActivity.this, "token"),
					mProjectItem.getProjectId(), PreferencesUtil.getString(
							ConfirmFundActivity.this, "userId"), Config.SOURCE,
					Util.getImei(ConfirmFundActivity.this),
					new PostConfirmFund.SuccessCallback() {

						@Override
						public void onSuccess(String msg, String orderNum) {
							mProgressDialog.dismiss();
							mTvProjectName.setText(mProjectItem
									.getProjectName());
							mTvProjectOrder.setText(orderNum);
							// TODO 图片以及其他细节待处理
						}
					}, new PostConfirmFund.FailCallback() {

						@Override
						public void onFail(String msg) {
							mProgressDialog.dismiss();
							Toast.makeText(ConfirmFundActivity.this, msg,
									Toast.LENGTH_SHORT).show();
						}
					}, ConfirmFundActivity.this);
		} else {
			Toast.makeText(this,
					getResources().getString(R.string.check_your_network),
					Toast.LENGTH_SHORT).show();
		}
	}
}
